package com.ncst.mvcweb.controller;

import com.ncst.mvcweb.entity.OrderItem;
import com.ncst.mvcweb.entity.ProductOrder;
import com.ncst.mvcweb.entity.User;
import com.ncst.mvcweb.service.OrderItemService;
import com.ncst.mvcweb.service.ProductOrderService;
import com.ncst.mvcweb.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class ProductOrderController {
    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private OrderItemService orderItemService;

    /**
     * 查询订单列表 - 支持用户查询自己的订单和管理员查询所有订单
     * @param pageSize 每页显示的条数
     * @param pageNumber 页码
     * @param session 登录用户
     * @return
     */
    @GetMapping(value = "/list")
    public PageResult<ProductOrder> list(int pageSize, int pageNumber, HttpSession session) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "orderTime");
        PageRequest page = PageRequest.of(pageNumber, pageSize, Sort.by(order));
        if (session.getAttribute("loginUser") == null) {
            return null;
        }
        else {
            User user = (User)session.getAttribute("loginUser");
            // 检查是否为管理员（这里假设用户名为admin的用户为管理员）
            if ("admin".equals(user.getUsername())) {
                // 管理员查询所有订单
                Page<ProductOrder> page1 = productOrderService.findByPage(page);
                return new PageResult<>((int) page1.getTotalElements(), page1.getContent());
            } else {
                // 普通用户查询自己的订单
                Page<ProductOrder> page1 = productOrderService.findByUser(user, page);
                return new PageResult<>((int) page1.getTotalElements(), page1.getContent());
            }
        }
    }

    /**
     * 管理员查询所有订单的专用接口
     * @param pageSize 每页显示的条数
     * @param pageNumber 页码
     * @return
     */
    @GetMapping(value = "/admin/list")
    public PageResult<ProductOrder> adminList(int pageSize, int pageNumber) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "orderTime");
        PageRequest page = PageRequest.of(pageNumber, pageSize, Sort.by(order));
        Page<ProductOrder> page1 = productOrderService.findByPage(page);
        return new PageResult<>((int) page1.getTotalElements(), page1.getContent());
    }

    /**
     * 查询所有订单（不分页）- 用于批量查询
     * @return
     */
    @GetMapping(value = "/all")
    public List<ProductOrder> getAllOrders() {
        return productOrderService.findAll();
    }

    @GetMapping("/queryOrderById")
    public ProductOrder queryUserById(Integer id){
        return productOrderService.queryById(id);
    }

    @GetMapping("/findItemsByOrderId")
    public List<OrderItem> findItemsByOrderId(Integer id){
        if (id == null)
            return new ArrayList<>();
        return orderItemService.findByOrder(id);
    }
}
